package com.benefm.multipar.pad.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.benefm.multipar.pad.R;
import com.benefm.multipar.pad.dashboard.DashboardActivity;
import com.benefm.multipar.pad.event.BloodPressureEvent;
import com.benefm.multipar.pad.event.GyroscopeInfoEvent;
import com.benefm.multipar.pad.model.PersonInfo;
import com.benefm.multipar.pad.view.EcgSurfaceView;
import com.benefm.multipar.pad.view.EcgWaveData;

import java.util.ArrayList;
import java.util.List;

public class HomeItemView extends FrameLayout {

    private Context context;
    private TextView textName, textChannel;
    private TextView textHr, textSpo2, textBp, textPr, textBattery;
    private ImageView imagePosture, iconBattery;

    private EcgSurfaceView ecgView;
    private String tag;
    private PersonInfo info;

    private final int N = 150;
    private float[] hfData = new float[N];
    private int hfPtr = 0;
    private float hfy = 0;

    public HomeItemView(@NonNull Context context) {
        super(context);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.item_home_view, this);
        initView();
    }


    public HomeItemView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.item_home_view, this);
        initView();
    }

    @Override
    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    private void initView() {
        textName = findViewById(R.id.name);
        textChannel = findViewById(R.id.channel);
        textHr = findViewById(R.id.textHr);
        textSpo2 = findViewById(R.id.textSpo2);
        textBp = findViewById(R.id.textBp);
        textPr = findViewById(R.id.textPr);
        textBattery = findViewById(R.id.textBattery);
        imagePosture = findViewById(R.id.imagePosture);
        iconBattery = findViewById(R.id.iconBattery);

        ecgView = findViewById(R.id.ecgView);
        ecgView.setVisibility(INVISIBLE);
        imagePosture.setVisibility(INVISIBLE);
        findViewById(R.id.topLayout).setVisibility(INVISIBLE);
        findViewById(R.id.bottomLayout).setVisibility(INVISIBLE);
    }

    public PersonInfo getInfo() {
        return info;
    }

    public void setInfo(PersonInfo info) {
        this.info = info;
        ecgView.setVisibility(VISIBLE);
        findViewById(R.id.topLayout).setVisibility(VISIBLE);
        findViewById(R.id.bottomLayout).setVisibility(VISIBLE);

        iconBattery.setVisibility(INVISIBLE);
        textBattery.setVisibility(GONE);
        imagePosture.setVisibility(INVISIBLE);

        textName.setText(TextUtils.isEmpty(info.name) ? "--" : info.name);
        textChannel.setText(info.channelName);
        //重置一下数据
        textHr.setText("--");
        textSpo2.setText("--");
        textBp.setText("--/--");
        textPr.setText("--");

    }

    public void resetView() {
        this.info = null;
        this.tag = "";
        ecgView.setVisibility(INVISIBLE);
        findViewById(R.id.topLayout).setVisibility(INVISIBLE);
        findViewById(R.id.bottomLayout).setVisibility(INVISIBLE);
    }


    public void updateInfo(int dataType, String data) {
        //心率
        if (dataType == 1) {
            textHr.setText(data);
        } else if (dataType == 2) {//血氧
            textSpo2.setText(data);
        } else if (dataType == 3) {//血压
            textBp.setText(data);
        }
    }

    public void updateBpData(BloodPressureEvent bpEvent) {
        textBp.setText(bpEvent.sbp + "/" + bpEvent.dbp);
        textPr.setText(String.valueOf(bpEvent.pr));
    }

    public void updateGyroscopeInfo(GyroscopeInfoEvent infoEvent) {
        imagePosture.setVisibility(VISIBLE);
        if (infoEvent.degrees > 70.0) {
            imagePosture.setImageResource(R.drawable.ic_baseline_fall);
        } else {
            imagePosture.setImageResource(R.drawable.ic_baseline_stand);
        }
    }

    public void updateBattery(int battery) {
        iconBattery.setVisibility(VISIBLE);
//        textBattery.setVisibility(VISIBLE);
//        textBattery.setText(battery + "%");

        if (battery <= 100 && battery > 80) {
            iconBattery.setImageResource(R.drawable.ic_baseline_battery1);
        } else if (battery <= 80 && battery > 60) {
            iconBattery.setImageResource(R.drawable.ic_baseline_battery2);
        } else if (battery <= 60 && battery > 40) {
            iconBattery.setImageResource(R.drawable.ic_baseline_battery3);
        } else if (battery <= 40 && battery > 20) {
            iconBattery.setImageResource(R.drawable.ic_baseline_battery4);
        } else {
            iconBattery.setImageResource(R.drawable.ic_baseline_battery5);
        }

    }


    public void updateWave(List<Short> list) {
        ArrayList<EcgWaveData> data = new ArrayList<>();
        for (short f : list) {
            data.add(new EcgWaveData(1, lbo(Float.parseFloat(String.valueOf(f)))));
        }
        ecgView.notifyData(data);
    }


    public void setOnClickListener() {
        setOnClickListener(v -> {
            if (TextUtils.isEmpty(tag) || info == null)
                return;
//            Intent intent = new Intent(context, EcgWaveActivity.class);
//            intent.putExtra("userId", tag);
//            intent.putExtra("info", info);
//            context.startActivity(intent);

            Intent intent = new Intent(context, DashboardActivity.class);
            intent.putExtra("userId", tag);
            context.startActivity(intent);

        });
    }

    private float lbo(Float dataIn) {
        int halfPtr;
        float out = 0;
        hfy = hfy + dataIn - hfData[hfPtr];
        halfPtr = hfPtr - N / 2;

        if (halfPtr < 0) {
            halfPtr = halfPtr + N;
        }
        out = hfData[halfPtr] - (hfy / N);

        hfData[hfPtr] = dataIn;
        hfPtr = hfPtr + 1;
        if (hfPtr == N) {
            hfPtr = 0;
        }
        return out;
    }

}
